//
//  YMFile.swift
//  ProductViewDemo
//
//  Created by youmy on 2018/11/27.
//  Copyright © 2018 youmy. All rights reserved.
//

import Foundation

/// 将字典数组写入缓存
///
/// - Parameters:
///   - fileName: 文件名
///   - array: 字典数组
func writoFile(fileName: String, array:[[String:Any]],completion:(_ result:Bool)->()){
    let path = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
    let jsonPath = (path as NSString).appendingPathComponent("\(fileName).json")
    print("沙盒：\(jsonPath)")
    do{
        let data = try JSONSerialization.data(withJSONObject: array, options: []) as NSData
        data.write(toFile: jsonPath, atomically: true)
        completion(true)
    }catch _{
        print("文件写入失败：\(fileName).json")
        completion(false)
    }
}


/// 从缓存读取字典
///
/// - Parameter fileName: 文件名
/// - Returns: 字典数组
func readArrayForFile(fileName: String) -> Array<Dictionary<String, Any>>? {
    let path = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
    
    let jsonPath = (path as NSString).appendingPathComponent("\(fileName).json")
    
    let data = try? Data(contentsOf: URL(fileURLWithPath: jsonPath))
    
    if data != nil {
        let arr = try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! Array<Dictionary<String, Any>>
        return arr ?? nil
    }
    return nil
}

func writoFile(fileName: String, dic:[String:Any]) {
    
    let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    
    let jsonPath = (path as NSString).appendingPathComponent("\(fileName).json")
    print("沙盒：\(jsonPath)")
    do{
        let data = try JSONSerialization.data(withJSONObject: dic, options: []) as NSData
        data.write(toFile: jsonPath, atomically: true)
    }catch _{
        print("文件写入失败：\(fileName).json")
    }
}

func readFile(fileName: String) -> [String:Any] {
    let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    
    let jsonPath = (path as NSString).appendingPathComponent("\(fileName).json")
    
    let data = try? Data(contentsOf: URL(fileURLWithPath: jsonPath))
    
    if data != nil {
        let dic = try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! Dictionary<String, Any>
        return dic ?? [String: Any]()
    }
    return [String: Any]()
}

func resouceDataFromFile(fileName: String) -> Data? {
    
    let jsonPath = Bundle.main.path(forResource: fileName, ofType: nil)
    
    let data = try? Data(contentsOf: URL(fileURLWithPath: jsonPath!))
    
    return data
}

func resoucearrayFromFile(fileName: String) -> [[String:Any]] {
    let jsonPath = Bundle.main.path(forResource: fileName, ofType: nil)
    
    let data = try? Data(contentsOf: URL(fileURLWithPath: jsonPath!))
    
    if data != nil {
        let arr = try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! Array<Dictionary<String, Any>>
        return arr ?? [[String: Any]]()
    }
    return [[String: Any]]()
}

func resouceObjectFromFile(fileName: String) -> [String:Any] {
    let jsonPath = Bundle.main.path(forResource: fileName, ofType: nil)
    
    let data = try? Data(contentsOf: URL(fileURLWithPath: jsonPath!))
    
    if data != nil {
        let dic = try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! Dictionary<String, Any>
        return dic ?? [String: Any]()
    }
    return [String: Any]()
}

func arrayFromFile(fileName: String) -> [[String:Any]] {
    let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    
    let jsonPath = (path as NSString).appendingPathComponent("\(fileName).json")
    
    let data = try? Data(contentsOf: URL(fileURLWithPath: jsonPath))
    
    if data != nil {
        let arr = try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! Array<Dictionary<String, Any>>
        return arr ?? [[String: Any]]()
    }
    return [[String: Any]]()
}
